import {Label, Text} from '@kintone/kintone-ui-component/esm/js';
import * as tyc from '../api/TianyanchaAPI';
import {BaseConfig} from './BaseConfig';
import util from '../common/util';

export class TianyanchaConfig extends BaseConfig {
  constructor({pluginConfig, proxyConfig}) {
    super({
      pluginConfig,
      proxyConfig,
      server: tyc.SERVER,
      tableCells: tyc.API_RETURN_FIELD
    });
    this.tianyanchaToken = new Text({
      placeholder: 'token值',
      value: ('headers' in this.proxyConfig && 'Authorization' in this.proxyConfig.headers) ?
        this.proxyConfig.headers.Authorization : ''
    });
  }

  render() {
    const div = super.render();
    const authDiv = this.divElements[0];
    authDiv.className += ' company-info_display_flex';
    authDiv.appendChild(new Label({text: '天眼查API验证Token', isRequired: true}).render());
    authDiv.appendChild(this.tianyanchaToken.render());

    return div;
  }

  getTokenValue() {
    return this.tianyanchaToken.getValue();
  }

  checkValue() {
    if (util.isEmptyString(this.tianyanchaToken.getValue())) {
      return '请填写 "天眼查API验证Token"';
    }

    return super.checkValue();
  }
}